﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Text;
using System.Configuration;
namespace WebHelper.Pages
{
    public class Log
    {

        #region 记事本日志
        public static void WriteLog(string msg)
        {
            WriteLog(msg, null);
        }
        /// <summary>
        /// 写入文件日志
        /// </summary>
        /// <param name="meg"></param>
        public static void WriteLog(string meg, string type)
        {
            //写入日志
            if (string.IsNullOrEmpty(type))
                type = "MianWeb";
            string pathMain = ConfigurationManager.AppSettings["LogAddress"].ToString();
            pathMain = pathMain.Remove(pathMain.LastIndexOf('\\') + 1) + type.ToString() + "\\";
            string path = pathMain + "log" + DateTime.Now.ToString("yyyyMM") + @"\OperLog" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
            string directory = pathMain + "log" + DateTime.Now.ToString("yyyyMM");
            DirectoryInfo dif = new DirectoryInfo(directory);

            if (!dif.Exists)
            {
                dif.Create();
            }
            FileInfo fileinfo = new FileInfo(path);
            if (!fileinfo.Exists)
            {
                File.WriteAllText(fileinfo.FullName, "", Encoding.UTF8);
            }

            using (FileStream fs = fileinfo.OpenWrite())
            {
                StreamWriter sw = new StreamWriter(fs);

                sw.BaseStream.Seek(0, SeekOrigin.End);
                sw.WriteLine(meg);

                sw.Flush();
                sw.Close();
            }
        }

        #endregion
        }
}